/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2021-01-08
*/
package com.rzico.article.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.article.entity.Article;
import com.rzico.article.entity.ArticleLaud;
import com.rzico.article.service.ArticleLaudService;
import com.rzico.article.service.ArticleService;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Member;
import com.rzico.basics.service.MemberService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 笔记点赞控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2021-01-08
 */
@Api(description = "笔记点赞接口")
@RestController
@RequestMapping("/articleLaud")
public class ArticleLaudController extends BaseController {

    @Autowired
    private ArticleLaudService articleLaudService;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private MemberService memberService;
    /**
     * 分页查询笔记点赞
     *
     * @return
    */
    @ApiOperation("分页查询单个笔记点赞")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "articleId", value = "文章Id", dataType = "String", paramType = "query")
    })
    public CommResult<ArticleLaud> list(Date startDate, Date endDate, String articleId, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        Map<String,Object> data = new HashMap<>();
        Member member = memberService.getCurrent();
        params = buildSortField(params, pageable);
        if (startDate!=null){
           params.put("startDate", startDate);
        }
        if (endDate!=null){
           params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }
        if (StringUtils.isNotBlank(articleId)) {
            params.put("articleId", articleId);
        }
        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<ArticleLaud> list = articleLaudService.selectList(params);
        PageResult<ArticleLaud> pageResult = new PageResult<ArticleLaud>(list, startPage.getTotal(), pageable);

        //查询自己是否已点赞
        Map<String,Object> params2 = new HashMap<>();
        if (member.getId() !=null && member.getId() > 0 && params.containsKey("articleId")) {
            params2.put("articleId", articleId);
            params2.put("memberId", member.getId());
        }
        List<ArticleLaud> articleLauds = articleLaudService.selectList(params2);
        if (articleLauds != null && articleLauds.size() != 0) {
            data.put("isLaud",true);
        } else {
            data.put("isLaud", false);
        }
        data.put("pageable", pageResult);
        return CommResult.success(data);
    }

    /**
     * 查看他人的点赞
     */
    @ApiOperation("查看达人的点赞")
    @GetMapping("/find")
    public CommResult<ArticleLaud> show(Long talentId,Integer articleType, Pageable pageable) {

        //查出这个达人的memberID
        List<Article> articles = articleService.showOtherLaudArticle(talentId,articleType,pageable);
        return CommResult.success(articles);
    }
}
